package com.jieshuibao.service;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jieshuibao.mapper.AnswerMapper;
import com.jieshuibao.mapper.AnswerUserMapper;
import com.jieshuibao.mapper.QuestionExMapper;
import com.jieshuibao.mapper.QuestionMapper;
import com.jieshuibao.model.db.Answer;
import com.jieshuibao.model.db.AnswerExample;
import com.jieshuibao.model.db.AnswerUser;
import com.jieshuibao.model.db.Question;
import com.jieshuibao.model.db.QuestionEx;
import com.jieshuibao.model.db.QuestionExample;
import com.jieshuibao.model.db.User;
import com.jieshuibao.model.domain.PagePhone;

@Service
public class AnswerUserService {
	@Autowired
	private AnswerUserMapper answerUserMapper;
	@Resource
	private QuestionMapper questionMapper;
	@Resource
	private AnswerMapper answerMapper;
	@Resource
	private QuestionExMapper questionExMapper;
	//偷偷看详情
	public List<AnswerUser> getAnswerUsers(AnswerExample example){
		return answerUserMapper.selectAnswerUserByExample(example);
	}
	
	public List<AnswerUser> getAnswerUsersByQuestionUuid(PagePhone pagePhone){
		return answerUserMapper.selectAnswerUserByQuestionUuid(pagePhone);
	}
	//查看赞数
	public List<Answer> getAnswer(String uuid,int status){
		return answerUserMapper.selectAnswerApproveNum(uuid, status);
	}
	//根据问题的uuid和用户的账号查询答案的状态
	public Answer getAnswerStatus(String QUuid,String phoneNum){
		return answerUserMapper.selectStatusByIdAndPhoneNum(QUuid, phoneNum);
				
	}
	//根据问题的uuid查询已采纳的答案
	public List<AnswerUser> getAnswerUserByQUid(String questionUuid){
		return answerUserMapper.selectAnswerUserByQUid(questionUuid);
	}
	
	public List<AnswerUser> getAnswerUserByPhoneAndQUuid(String phoneNum,String QUuid){
		return answerUserMapper.selectAnswerUserByPhoneAndQUuid(phoneNum, QUuid);
		
	}
	//提交答案
	public void updateAnswerAndQuestion(Answer answer,Question question){
		if(answer!=null){
			answerMapper.updateByPrimaryKeySelective(answer);
		}
		if(question!=null){
			questionExMapper.updateByPrimaryKeySelective(question);
		}
	}
	public void addAnswerAndQuestion(Answer answer,Question question){
		if(answer!=null){
			answerMapper.insertSelective(answer);
		}
		if(question!=null){
			questionMapper.updateByPrimaryKeySelective(question);
		}
		
		
	}
		//是否有未提交的抢答
	public List<Answer> getAnswerByPhone(String phoneNum){
		return answerUserMapper.selectAnswrsByPhone(phoneNum);
	}
	
	/**
	 * 是否有未提交的答案
	 * @param phoneNum
	 * @return
	 */
	public boolean isAnswerWait(String phoneNum){
		
		List<Answer> list = answerUserMapper.selectAnswrsByPhone(phoneNum);
		if (list.size()>0) {
			return true;
		}else {
			return false;
		}
		
	}
	
	
	// 未被采纳的 人的信息
	public List<User> getAnswerUserByQuestionId(String id){
		return answerUserMapper.selectUserByQuestionUuid(id);
	}
	//修改答案的点赞
	public void updateAnswerApproveNum(Answer record) {
		answerUserMapper.updateByPrimaryKeySelective(record);
		
	}
	
	//
	/**
	 * 查询个人被采纳的问题
	 * @param answerExample
	 * @param questionExample
	 * @return
	 */
	public List<QuestionEx> getUserQuestionAadops(AnswerExample answerExample,QuestionExample questionExample){
		return answerUserMapper.selectUserAnswerAdopts(answerExample, questionExample);
	}
}
